package com.cortex.carambola.listener;

import com.cortex.carambola.view.ShowSummariesView;
import com.cortex.carambolasummary.R;

import android.graphics.Bitmap;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class DragDropView implements OnTouchListener{
	private boolean startFlag = false;
	private ImageView image;
	private View view;
	private RelativeLayout.LayoutParams params;
	private DragDropStateChangedListener dragDropStateChangedListener;
	private int topMax,leftMax,rightMax,bottomMax;

	
	public DragDropView(View view){
		this.view = view;
		view.setDrawingCacheEnabled(true);
		image = new ImageView(view.getContext());
		params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}
	@Override
	public boolean onTouch(View view, MotionEvent event) {
		final int x = (int) event.getRawX();
		final int y = (int) event.getRawY();
		if (startFlag) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				// create copy bitmap and put it into image
				image.setImageBitmap(Bitmap.createBitmap(view.getDrawingCache()));
				view.setVisibility(View.GONE);
				positionChanged(x, y);
				if(dragDropStateChangedListener != null){
					dragDropStateChangedListener.drop(view,image);
				}
				return true;
			}
			else if (event.getAction() == MotionEvent.ACTION_MOVE) {

				positionChanged(x, y);
				if(dragDropStateChangedListener != null){
					dragDropStateChangedListener.drag(view,image.getTop(),
							image.getLeft(),image.getRight(),image.getBottom());
				}
			}
			else if (event.getAction() == MotionEvent.ACTION_UP) {
				view.setVisibility(View.VISIBLE);
				if(dragDropStateChangedListener != null){
					dragDropStateChangedListener.stopDragDrop(view,image);
				}
				params.leftMargin = 0;
				params.rightMargin = 0;
				stop();
			} 
		}
		return false;
	}
	private void positionChanged(int x,int y){
		// calculate limits of screen that view can move
		// when we arrived to here we not sure that the image inflated so we use in height of view 
		int topMargin = y - (view.getHeight());
		int leftMargin = x; 

		if(topMargin<topMax){
			topMargin = topMax;
		}
		else if(topMargin>bottomMax){
			topMargin = bottomMax;
		}
		params.topMargin = topMargin;
		params.leftMargin = leftMargin;
		params.bottomMargin = -image.getHeight();
		params.rightMargin = -image.getWidth();
		image.setLayoutParams(params);
		image.invalidate();
	}
	public void start(){
		startFlag = true;
	}
	public void stop(){
		startFlag = false;
	}
	public void setScreenLimits(int top,int left,int right,int bottom){
		topMax = top;
		leftMax = left;
		rightMax = right;
		bottomMax = bottom;
	}
	public void setDragDropStateChangedListener(DragDropStateChangedListener dragDropStateChangedListener){
		this.dragDropStateChangedListener = dragDropStateChangedListener;
	}
	public interface DragDropStateChangedListener{
		void drop(View v,View copyView);
		void drag(View v, int top,int left,int right,int bottom);
		void stopDragDrop(View v,View copyView);
	}
}
